package com.ljw.gulimall.cart.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ljw.common.utils.GetCookieUtils;
import com.ljw.gulimall.cart.interceptor.CartInterceptor;
import com.ljw.gulimall.cart.service.CartService;
import com.ljw.gulimall.cart.to.UserInfoTo;
import com.ljw.gulimall.cart.vo.Cart;
import com.ljw.gulimall.cart.vo.CartItem;
import com.ljw.gulimall.cart.vo.MemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 获取登入用户的购物车数据，为订单模块服务
     * @return
     */
    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItem> getCurrentUserItems(){
        return cartService.getCurrentUserItems();
    }

    /**
     * 删除购物项
     * @param skuId
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }


    /**
     * 增加或减少商品数量
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num){

        cartService.countItem(skuId, num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 修改购物车中的购物项是否被选中
     * @param skuId
     * @param check
     * @return
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("check") Integer check){
        cartService.checkItem(skuId, check);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 购物车详情页
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model, HttpServletRequest request){
        Cart cart = cartService.getCart();
        model.addAttribute("cart", cart);
        String memberIdByJwtToken = GetCookieUtils.getMemberIdFormCookie(request);

        //控制提示用户登入框的显示
        model.addAttribute("userId", memberIdByJwtToken);
        //用户登入把用户添加到模版中去
        if (memberIdByJwtToken != null){
            String memberInfoJson = redisTemplate.opsForValue().get(memberIdByJwtToken);
            MemberVo memberVo = JSON.parseObject(memberInfoJson, new TypeReference<MemberVo>() {
            });
            model.addAttribute("loginUser", memberVo);
            System.out.println(memberInfoJson);
        }
        return "cartList";
    }

    /**
     * 添加商品到购物车
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes redirectAttributes){
        cartService.addToCart(skuId, num);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId, Model model, HttpServletRequest request){
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("item", cartItem);

        String memberIdByJwtToken = GetCookieUtils.getMemberIdFormCookie(request);
        if (memberIdByJwtToken != null){
            String memberInfoJson = redisTemplate.opsForValue().get(memberIdByJwtToken);
            MemberVo memberVo = JSON.parseObject(memberInfoJson, new TypeReference<MemberVo>() {
            });
            model.addAttribute("loginUser", memberVo);
            System.out.println(memberInfoJson);
        }
        return "success";
    }
}
